//
//  UserInfoBackCell.m
//  ChatForHot
//
//  Created by FLY on 2018/10/14.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "UserInfoBackCell.h"
#import "PicOrVideoCell.h"

#import <AVFoundation/AVFoundation.h>

@implementation UserInfoBackCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self addSubview:self.HOTCHATbackImgView];
        [self addSubview:self.HOTCHATwaitImgView];
        
        [self.HOTCHATbackImgView mas_updateConstraints:^(MASConstraintMaker *make) {
           
            make.edges.equalTo(self.contentView);
        }];
        [self.HOTCHATwaitImgView mas_updateConstraints:^(MASConstraintMaker *make) {
           
            make.center.equalTo(self.contentView);
        }];
    }
    
    return self;
}
- (void)setModel:(VPModel *)model{
    
    _model = model;
    
    if (model.isVideo) {
        
        self.HOTCHATwaitImgView.hidden = NO;
        [self HOTCHATthumbnailVidoImg:model.url];
    }else{
        
        self.HOTCHATwaitImgView.hidden = YES;
        [self.HOTCHATbackImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIHEADER,model.url]] placeholderImage:[UIImage imageNamed:@"personal_atatar_but_h"]];
    }
}
- (void)HOTCHATthumbnailVidoImg:(NSString *)url {
    self.HOTCHATbackImgView.image = [UIImage imageNamed:@"personal_atatar_but_h"];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",APIHEADER,url];
    NSURL *videoURL = [NSURL URLWithString:urlStr];
    
    YYImageCache *chches = [YYImageCache sharedCache];
    __block UIImage *thumbImg = [chches getImageForKey:videoURL.absoluteString];
    
    if (thumbImg == nil) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
            AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
            gen.appliesPreferredTrackTransform = YES;
            CMTime time = CMTimeMakeWithSeconds(1.0, 600);
            NSError *error = nil;
            CMTime actualTime;
            CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
            thumbImg = [[UIImage alloc] initWithCGImage:image];
            
            [chches setImage:thumbImg forKey:videoURL.absoluteString];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([url isEqualToString:self.model.url]) {
                    if (thumbImg) {
                        self.HOTCHATbackImgView.image = thumbImg;
                    }
                }
            });
        });
    }else{
        if ([url isEqualToString:self.model.url]) {
            self.HOTCHATbackImgView.image = thumbImg;
            [self.HOTCHATbackImgView setNeedsLayout];
        }
    }
    
    
}
//lazzy
- (UIImageView *)HOTCHATbackImgView{
    
    if (_HOTCHATbackImgView == nil) {
        
        _HOTCHATbackImgView = [[UIImageView alloc]init];
        _HOTCHATbackImgView.contentMode = UIViewContentModeScaleAspectFill;
        _HOTCHATbackImgView.clipsToBounds = YES;
    }
    return _HOTCHATbackImgView;
}
- (UIImageView *)HOTCHATwaitImgView{
    
    if (_HOTCHATwaitImgView == nil) {
        
        _HOTCHATwaitImgView = [[UIImageView alloc]init];
        _HOTCHATwaitImgView.contentMode = UIViewContentModeScaleAspectFill;
        _HOTCHATwaitImgView.image = [UIImage imageNamed:@"waitplay"];
        _HOTCHATwaitImgView.highlightedImage = [UIImage imageNamed:@"waitplay_hig"];
    }
    return _HOTCHATwaitImgView;
}
@end
